<?php

namespace App\Http\Services;


use App\Http\Model\AudioApi;
use App\Http\Model\Gift;
use App\Http\Model\LightsCard;
use App\Http\Model\Medal;
use App\Http\Model\User;

class LightsCardServices extends CommonServices
{
    private $lights_card_model;
    private $user_model;
    private $audio_model;
    private $gift_model;
    private $medal_model;
    private $medal_id = 88;
    private $prop_id = 20;


    public function __construct()
    {
        $this->lights_card_model = new LightsCard();
        $this->user_model = new User();
        $this->audio_model = new AudioApi();
        $this->gift_model = new Gift();
        $this->medal_model = new Medal();
    }


    /*
     * 购买点亮卡
     * @param $user_id 用户id
     * @param $num 购买数量
     * */
    public function payLightsCard($user_id, $num)
    {
        //获取用户钻石数量
        $pay_diamond = 200;
        $all_diamond = $pay_diamond * $num;
        $status = [
            'code' => 0,
            'msg' => '成功',
            'data' => [],
        ];
        do {
            $redisKeyLock = 'CACHE:LIGHTS' . $user_id;
            if (redis_lock($redisKeyLock, 2) == false) {
                $status = [
                    'code' => 3002,
                    'msg' => '手速太快啦~',
                    'data' => ''
                ];
                break;
            }
            if ($num < 1 || $num > 99) {
                $status = [
                    'code' => 3001,
                    'msg' => '请购买1-99张点亮卡',
                    'data' => [],
                ];
                break;
            }
            //扣除用户钻石数量
            $pay_res = $this->audio_model->diamond_recharge($user_id, $all_diamond, 4);
            if ($pay_res['code'] != 0) {
                $status = [
                    'code' => $pay_res['code'],
                    'msg' => $pay_res['msg'],
                    'data' => [],
                ];
                break;
            }
            $award_gift_log = [];
            while ($num > 0) {
                $gift_id = $this->lights_card_model->popPond();
                if ($gift_id == null) {
                    $this->lights_card_model->createPond();
                    $gift_id = $this->lights_card_model->popPond();
                }
                //获取礼物类型
                $type = $this->lights_card_model->checkGiftType($gift_id);
                //获取礼物优先级
                $level = $this->lights_card_model->getGiftLevel($gift_id);

                switch ($type) {
                    case 1://头饰
//                        $this->audio_model->sendUsedMall($user_id, $gift_id, 1, 1);
                        break;
                    case 2://座驾
//                        $this->audio_model->sendUsedMall($user_id, $gift_id, 2, 1);
                        break;
                    case 3://礼物
                        $this->audio_model->sendGift($user_id, $gift_id, 17, 1);
                        break;
                    case 4://道具卡
                        //赠送道具卡
                        $this->audio_model->sendAllProp($user_id, $gift_id, 3, 1, 9, 0);
                        //飘屏
                        $user_info = $this->user_model->getUserInfoByApi($user_id);
                        $msg = '恭喜' . $user_info['nickname'] . '在心愿卡活动中获得点亮卡,可以点亮礼物墙任意礼物';
                        $this->audio_model->sendAllAppApi($user_id, 0, $msg);
                        break;

                }
                $gift_info = $this->getAwardInfo($gift_id, $type);
                $award_gift_log[] = [
                    'name' => $gift_info['name'],
                    'img' => $gift_info['img'],
                    'type' => $type,
                    'level' => $level,
                ];
                //总开启记录
                $this->lights_card_model->createAwardLog($user_id, $gift_id, $type);
                $num--;
            }
            $flag = [];
            foreach ($award_gift_log as $v) {
                $flag[] = $v['level'];
            }
            array_multisort($flag, SORT_ASC, $award_gift_log);;
            $status['data'] = $award_gift_log;
            redis_unlock($redisKeyLock);
            break;
        } while (false);
        return $status;
    }


    //获取获奖记录
    public function awardLog(int $user_id)
    {
        $cache = $this->lights_card_model->getAwardLog($user_id);
        $i = 0;
        $data = [];
        foreach ($cache as $k => $v) {
            $key = explode('-', $k);
            $gift_info = $this->getAwardInfo($key[0], $v);
            $data[$i] = [
                'name' => $gift_info['name'],
                'date' => date('m' . '月' . 'd' . '日', $key[1]),
                'type' => $v,
            ];
            $i++;
        }
        return array_reverse($data);
    }

    //获取用户相关信息
    public function getInfo(int $user_id)
    {
        $finance = $this->user_model->getUserFinance($user_id);
        $balance = $finance['diamond_balance'];
        if ($balance >= 1000) {
            $balance = floor($balance / 1000) . 'k';
        }
        $is_medal = 0;
        $medal = $this->medal_model->getUserMedal($user_id, $this->medal_id);
        if ($medal) {
            $is_medal = 1;
        }
        $status = [
            'is_medal' => $is_medal,//勋章是否可领取 0不可领取 1可以领取 2已经领取
            'balance' => $balance,//用户资产
            'activity_date' => '时间:2月26日0点-3月1日0点'//活动时间
        ];
        return $status;
    }

    public function getAwardInfo($gift_id, $type)
    {
        $data = [];
        switch ($type) {
            case 1://头饰
                $gift = json_decode($this->gift_model->getMallInfoCache($gift_id), true);
                $data['name'] = $gift['goods_name'];
                $data['img'] = build_image($gift['small_img']);
                break;
            case 2://座驾
                $gift = json_decode($this->gift_model->getMallInfoCache($gift_id), true);
                $data['name'] = $gift['goods_name'];
                $data['img'] = build_image($gift['small_img']);
                break;
            case 3://礼物
                $gift = json_decode($this->gift_model->getGiftInfoCache($gift_id), true);
                $data['name'] = $gift['name'];
                $data['img'] = $gift['image'];
                $data['price'] = $gift['price'];
                break;
            case 4://道具卡
                $gift = json_decode($this->gift_model->getPropInfoCache($gift_id), true);
                $data['name'] = $gift['name'];
                $data['img'] = $gift['image'];
                break;
        }
        return $data;
    }

    //获取礼物
    public function getGift($user_id)
    {
        $giftGiftIds = $this->gift_model->getLightsGift($user_id);
        $giftGiftIds = explode(',', $giftGiftIds);
        $getAllGift = $this->gift_model->getAllGiftInfo($user_id, $giftGiftIds);
        $getAllGift = json_decode($getAllGift, true);
        foreach ($getAllGift as & $v) {
            $v['image'] = build_image($v['image']);
        }
//        $getAllGift =  array_slice($getAllGift,0,17);
        return $getAllGift;
    }

    public function lightsGift($user_id, $gift_id)
    {
        $status = [
            'code' => 0,
            'msg' => '成功',
        ];
        do {
            $this->gift_model->delLightCache($user_id);
            $giftGiftIds = $this->gift_model->getLightsGift($user_id);
            $giftGiftIds = explode(',', $giftGiftIds);
            if (in_array($gift_id, $giftGiftIds)) {
                $status = [
                    'code' => 3001,
                    'msg' => '该礼物已点亮，请更换礼物~',
                ];
                break;
            }
            //点亮礼物
            $res = $this->audio_model->lightsGift($user_id, $gift_id, $this->prop_id);
            $status = [
                'code' => $res['code'],
                'msg' => $res['msg'],
            ];
            if ($res['code'] == 0) {
                //清空未点亮缓存
                $this->gift_model->delLightCache($user_id);
            }
            break;
        } while (false);
        return $status;
    }

    public function getMedal($user_id)
    {
        $status = [
            'code' => 3001,
            'msg' => '领取失败',
        ];
        do {
            //获取用户已点亮礼物
            $giftLightsGiftIds = $this->gift_model->getLightsGift($user_id);
            $lightsGiftIds = explode(',', $giftLightsGiftIds);
            //获取总礼物数据
            $getAllGiftIds = $this->gift_model->getAllGiftList();
            $getAllGiftIds = json_decode($getAllGiftIds, true);
            $giftIds = array_column($getAllGiftIds, 'gift_id');
            //判断是否点亮全部礼物
            $giftDiff = array_diff($giftIds, $lightsGiftIds);
            if ($giftDiff) {
                $status = [
                    'code' => 3001,
                    'msg' => '领取失败,您未点亮所有礼物',
                ];
                break;
            }
            //查看是否获取勋章
            $medal = $this->medal_model->getUserMedal($user_id, $this->medal_id);
            if ($medal) {
                $status = [
                    'code' => 3001,
                    'msg' => '领取失败,您已经获得该勋章',
                ];
                break;
            }
            //赠送勋章
            $res = $this->audio_model->sendMedal($user_id, $this->medal_id, 0);
            $status = [
                'code' => $res['code'],
                'msg' => $res['msg'],
            ];
            break;
        } while (false);
        return $status;
    }

    public function prop($num, $del)
    {
        $gift_price = 0;
        $card_mum = 0;
        $partake = $num * 200;
        if ($del == 1) {
            $this->lights_card_model->delPond();
        }
        while ($num > 0) {
            $gift_id = $this->lights_card_model->popPond();
            if ($gift_id == null) {
                $this->lights_card_model->createPond();
                $gift_id = $this->lights_card_model->popPond();
            }
            //获取礼物类型
            $type = $this->lights_card_model->checkGiftType($gift_id);
            //获取礼物优先级
            if ($type == 4) {
                $card_mum++;
            } else {
                $gift_info = $this->getAwardInfo($gift_id, $type);
                $gift_price = $gift_price + $gift_info['price'];
            }
            //总开启记录
            $num--;
        }
        echo '参与钻石' . $partake;
        echo '<br/>';

        echo '礼物价值' . $gift_price;
        echo '<br/>';
        echo '点亮卡次数' . $card_mum;
    }
}
